import java.util.Map;
import java.util.TreeMap;

public class TreeMapDemo12 {
    public static void main(String[] args) {
//        Map<String, String> map = new TreeMap<>();
//        map.put("1", "a");
//        map.put("6", "7");
//        map.put("2", "8");
//
//        for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
//            System.out.println(stringStringEntry.getKey());
//        }

        Map<Cat, Object> map1 = new TreeMap<>();
        map1.put(new Cat("加菲猫", 12), null);
        map1.put(new Cat("流浪猫", 10), null);
        map1.put(new Cat("流浪猫", 13), null);

        for (Map.Entry<Cat, Object> catObjectEntry : map1.entrySet()) {
            System.out.println(catObjectEntry.getKey());
        }

    }
}

class Cat implements Comparable<Cat> {
    private String name;
    private int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Cat o) {
        if (this.name.equals(o.name)) {
            return o.age - this.age;// 降序
        }
        return this.name.compareTo(o.name);// 升序
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
